package com.example.lishop.controller

import com.example.lishop.base.MyResponse
import com.example.lishop.entity.BUserEntity
import com.example.lishop.entity.bean.BaseListBean
import com.example.lishop.service.LiValid
import com.example.lishop.service.impl.UserServiceImpl
import io.swagger.annotations.Api
import io.swagger.annotations.ApiOperation
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.data.domain.Page
import org.springframework.validation.annotation.Validated
import org.springframework.web.bind.annotation.*

/**
 * Created on 2021/3/14 18:28
 * @author shuguo
 * 商家
 */
@Api(value = "/User", tags = ["用户接口"])
@RestController
@RequestMapping("/user")
class UserCtl {

    @Autowired
    lateinit var service: UserServiceImpl

    @ApiOperation(value = "用户注册")
    @PostMapping(value = ["/register"])
    fun register(@RequestBody @Validated(value = [LiValid.Second::class]) bean:BUserEntity) : MyResponse<BUserEntity> {
        return MyResponse(200, "注册成功", service.add(bean))
    }

    @ApiOperation(value = "用户登录")
    @PostMapping(value = ["/login"])
    fun login(@RequestBody @Validated(value = [LiValid.Second::class]) bean: BUserEntity) : MyResponse<BUserEntity> {
        return MyResponse(200, "登录成功", service.detailByPhone(bean))
    }

    @ApiOperation(value = "修改用户")
    @PostMapping(value = ["/update"])
    fun update(@RequestBody @Validated(value = [LiValid.First::class]) bean:BUserEntity) : MyResponse<BUserEntity> {
        service.update(bean)
        return MyResponse(200, "修改成功", detail(bean).data)
    }

    @ApiOperation(value = "用户列表")
    @PostMapping(value = ["/list"])
    fun list(@RequestBody @Validated bean: BaseListBean) : MyResponse<Page<BUserEntity>> {
        return MyResponse(200, "请求列表成功", service.list(bean.page, bean.size))
    }

    @ApiOperation(value = "删除用户")
    @PostMapping(value = ["/del"])
    fun del(@RequestBody @Validated(value = [LiValid.First::class]) bean:BUserEntity) : MyResponse<Unit> {
        return MyResponse(200, "删除成功", service.del(bean))
    }

    @ApiOperation(value = "查询用户")
    @PostMapping(value = ["/detail"])
    fun detail(@RequestBody @Validated(value = [LiValid.First::class]) bean:BUserEntity) : MyResponse<BUserEntity> {
        return MyResponse(200, "查找成功", service.detail(bean))
    }
}